package com.caojing.haolemai.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.R;
import com.caojing.haolemai.bean.HomeLvBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by CaoJing on 2016/10/18.
 */
public class HomeLvAdapter extends BaseAdapter {
    private List<HomeLvBean.ResultsBean> mlist;
    private Context mContext;

    public HomeLvAdapter(List<HomeLvBean.ResultsBean> list, Context mContext) {
        this.mlist = list;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mlist != null ? mlist.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return mlist != null ? mlist.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertview, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertview == null) {
            convertview = LayoutInflater.from(mContext).inflate(R.layout.home_fragment_lv_item, null);
            holder= new ViewHolder(convertview);
            convertview.setTag(holder);
        }else{
            holder = (ViewHolder) convertview.getTag();
        }
        Glide.with(mContext).load(mlist.get(position).getAdvertPic()).centerCrop().placeholder(R.mipmap.global_default_image_outlets).error(R.mipmap.global_default_image_outlets).into(holder.homeFragmentLvItemIv);
        holder.homeFragmentLvItemTvTips.setText(mlist.get(position).getTips());
        holder.homeFragmentLvItemTvContent.setText(mlist.get(position).getContents());
        return convertview;
    }

    static class ViewHolder {
        @Bind(R.id.home_fragment_lv_item_iv)
        ImageView homeFragmentLvItemIv;
        @Bind(R.id.home_fragment_lv_item_tvTips)
        TextView homeFragmentLvItemTvTips;
        @Bind(R.id.home_fragment_lv_item_tvContent)
        TextView homeFragmentLvItemTvContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
